import URL from './url.js'
import base64 from 'base-64'
import { Alert } from 'react-native';
import { MapView, MapTypes, Geolocation } from 'react-native-baidu-map';
export default class discovery {
    //通过云课堂课程id获取班级列表信息（含班级号）
    getClassList(siteid) {
        let keymap = {
            method: 'GET',
            headers: { 'siteid': siteid },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.getMemberListbySite_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        var out = {};
                        resultData.forEach((item) => {
                            out[item.classcode] = [];
                        });
                        resultData.forEach((item) => {
                            out[item.classcode].push(item);
                        });
                        let outArray = []
                        for (let key in out) {//key就是学期，例201701，将key变为term，进而将title变为term即可  
                            outArray.push({ title: key, content: out[key] });
                            console.log('outArray');
                            console.log(outArray);
                        }
                        if (outArray != undefined && outArray.length > 0) {
                            resolve(outArray);
                        } else {
                            resolve(undefined);
                        }
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //开启课堂
    openCourse(userName, courseid, classList) {
        let keymap = {
            method: 'GET',
            headers: { 'userID': userName, 'moocCourseID': courseid, 'classList': classList, 'longitude': '114', 'latitude': '30' },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.openCourse_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        resolve(resultData);
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //关闭课堂 CloseCourse_URL
    closeCourse(moocCourseID) {
        let keymap = {
            method: 'GET',
            headers: { 'moocCourseID': moocCourseID, },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.CloseCourse_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        resolve(resultData.errorCode);
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //判断课堂开启状态，决定对应图标呈现 
    CheckOnlineClassIcon(moocCourseID) {
        let keymap = {
            method: 'GET',
            headers: { 'moocCourseID': moocCourseID, },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.CheckOnlineClassIconStatus_URL, keymap).then((result) => {
                result.text().then(
                    (resultData) => {
                        console.log('获取课堂开启状态', resultData)
                        resolve(resultData);
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //开启课堂签到
    openSignIn(userID, moocCourseID, longitude, latitude, TeacherName, CourseTitle) {
        let keymap = {
            method: 'GET',
            headers: { 'userID': userID, 'moocCourseID': moocCourseID, 'longitude': longitude, 'latitude': latitude, 'TeacherName': encodeURI(TeacherName), 'CourseTitle': encodeURI(CourseTitle), },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.openSigIn_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        console.log('开启签到结果', resultData)
                        resolve(resultData);
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //判断签到开启状态，决定对应图标呈现 返回值：0（已开启签到），1（未开启签到）
    CheckSignInIcon(moocCourseID) {
        let keymap = {
            method: 'GET',
            headers: { 'moocCourseID': moocCourseID, },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.CheckSignInIcon_URL, keymap).then((result) => {
                result.text().then(
                    (resultData) => {
                        console.log('获取签到开启状态', resultData)
                        resolve(resultData);
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }

    //关闭课堂签到
    closeSignIn(moocCourseID) {
        let keymap = {
            method: 'GET',
            headers: { 'moocCourseID': moocCourseID, },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.closeSignIn_URL, keymap).then((result) => {
                result.text().then(
                    (resultData) => {
                        console.log('关闭签到结果', resultData)
                        resolve(resultData);
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //学生端执行签到
    studentSignIn(moocCourseID,userID,StuName,Longitude,Latitude) {
        let keymap = {
            method: 'GET',
            headers: { 'moocCourseID': moocCourseID, 'userID': userID, 'StuName':encodeURI(StuName) , 'Longitude': Longitude, 'Latitude': Latitude, },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.StudentSignIn_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        console.log('学生端签到返回结果', resultData)
                        resolve(resultData);
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //学生端最新签到状态
    studentCheckSignInIconStatus(moocCourseID,StuID) {
        let keymap = {
            method: 'GET',
            headers: { 'moocCourseID': moocCourseID, 'StuID': StuID,},
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.CheckSignInIconStatus_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        console.log('学生端最新签到状态返回结果', resultData)
                        resolve(resultData);
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //教师端通过云课堂课程id获取签到列表
    getSignInStatistics(moocCourseID) {
        let keymap = {
            method: 'GET',
            headers: { 'moocCourseID': moocCourseID, },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.getSignInStatistics_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        console.log('教师端获取签到列表', resultData)
                        resolve(resultData);
                        // if (resultData != undefined && resultData.length > 0) {
                        //     resolve(resultData);
                        // } else {
                        //     resolve(undefined);
                        // }
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //通过用户id获取当前学期的课程列表
    getCourseList_currentTerm(user_id) {
        let keymap = {
            method: 'GET',
            headers: { 'UserName': user_id },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.getCurrentSiteList_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        if (resultData != undefined && resultData.length > 0) {
                            resolve(resultData);
                        } else {
                            resolve(undefined);
                        }
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //通过云课堂课程id获取上课号和对应交互课程id（课程-交互-教师端入口）
    getOnlineClassID_InteractionCourseID(moocCourseID) {
        let keymap = {
            method: 'GET',
            headers: { 'moocCourseID': moocCourseID },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.getOnlineClassID_InteractionCourseID_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        console.log('课程-交互-教师端-单击事件')
                        resolve(resultData);
                    }
                ).catch(
                    (error) => { reject(error) })
            }).catch((err) => { reject(err) })
        })
        return promise;
    }
    //通过mooc课程id获得交互课程id(暂时课程交互部分使用不到)
    getInteractionCourseID(course_id) {
        let keymap = {
            method: 'GET',
            headers: { 'moocCourseID': course_id },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.getInteractionCourseID_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        console.log('xxx', resultData)
                        if (resultData != undefined && resultData.length > 0) {
                            resolve(resultData[0].interactionCourseID);
                        } else {
                            resolve(undefined);
                        }
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //通过交互课程id获得章节及题目列表
    getCourseSubjectList(interactionCourseID) {
        let keymap = {
            method: 'GET',
            headers: { 'interactionCourseID': interactionCourseID },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.getCourseSubjectList_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        if (resultData != undefined && resultData.length > 0) {
                            resolve(resultData);
                        } else {
                            resolve(undefined);
                        }
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //通过题目id获取题目详细信息（包含题目title和选项content）
    getSubjectInfo(subjectId) {
        let keymap = {
            method: 'GET',
            headers: { 'subjectID': subjectId },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.getSubjectInfo_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        if (resultData != undefined && resultData.length > 0) {
                            resolve(resultData);
                        } else {
                            resolve(undefined);
                        }
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //推送题目 PushTopic_URL
    PushTopic(SubjectID, OnlineClassID, moocCourseID, TeacherName, CourseTitle) {
        let keymap = {
            method: 'GET',
            headers: { 'SubjectID': SubjectID, 'OnlineClassID': OnlineClassID, 'moocCourseID': moocCourseID, 'TeacherName': encodeURI(TeacherName), 'CourseTitle': encodeURI(CourseTitle), },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.PushTopic_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        console.log('接口推送结果', resultData)
                        console.log(resultData)
                        resolve(resultData);
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //判断推送题目状态，呈现对应图标（开启题目和关闭答题）
    CheckPushSubjectIcon(OnlineClassID, SubjectID) {
        let keymap = {
            method: 'GET',
            headers: { 'OnlineClassID': OnlineClassID, 'SubjectID': SubjectID, },
        };
        console.log('判断推送题目状态')
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.getSubjectStatus_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        console.log('获取推送题目状态', resultData)
                        resolve(resultData);
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }

    //学生端点击交互获取当前课程最新上课号，最新题目ID 以及推送题目号sendsubjectid
    get_OnlineClassIDSubjectIDSendSubjectID_Student(moocCourseID) {
        let keymap = {
            method: 'GET',
            headers: { 'moocCourseID': moocCourseID },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.studentAnswer_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        resolve(resultData);
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //通过题目id获取题目作答详情 
    getSubjectStatistic(subjectId) {
        let keymap = {
            method: 'GET',
            headers: { 'subjectID': subjectId },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.getSubjectStatistic_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        if (resultData != undefined && resultData.length > 0) {
                            resolve(resultData);
                        } else {
                            resolve(undefined);
                        }
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //学生端提交答案 
    studentSubmitAnswer(StuID, OnlineClassID, optionID, SubjectID, SendSubjectID) {
        let keymap = {
            method: 'GET',
            headers: { 'StuID': StuID, 'OnlineClassID': OnlineClassID, 'optionID': optionID, 'SubjectID': SubjectID, 'SendSubjectID': SendSubjectID, },
        };
        console.log('arg', keymap)
        var promise = new Promise((resolve, reject) => {
            fetch(URL.studentSubmitAnswer_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        resolve(resultData);
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //通过题目id获取正确的答案
    getSubjectAnswer(subject_id) {
        let keymap = {
            method: 'GET',
            headers: { 'subjectID': subject_id },
        };
        console.log('arg', keymap)
        let correctAnswer;
        var promise = new Promise((resolve, reject) => {
            fetch(URL.getSubjectAnswer_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        if (resultData.length == 0) {
                            correctAnswer = '无';
                            resolve(correctAnswer);
                        } else {
                            correctAnswer = obj;
                            resolve(correctAnswer);
                        }
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //通过上课号和题目id关闭答题
    closeSubject(OnlineClassID, SubjectID) {
        let keymap = {
            method: 'GET',
            headers: { 'OnlineClassID': OnlineClassID, 'SubjectID': SubjectID, },
        };
        console.log('arg', keymap)
        let correctAnswer;
        var promise = new Promise((resolve, reject) => {
            fetch(URL.CloseSubject_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        console.log('关闭答题123')
                        console.log(resultData)
                        resolve(resultData);
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })
        })
        return promise;
    }
    //通过题目号和题目id获取作答结果
    getSubjectStatistics(SendSubjectID, SubjectID) {
        let keymap = {
            method: 'GET',
            headers: { 'SendSubjectID': SendSubjectID, 'SubjectID': SubjectID, },
        };

        console.log('arg答题结果')
        console.log('arg', keymap)
        let correctAnswer;
        var promise = new Promise((resolve, reject) => {
            fetch(URL.getSubjectStatistics_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        console.log('推送题目作答结果')
                        console.log(resultData)
                        //  if (resultData != undefined && resultData.length > 0) {
                        //     resolve(resultData);
                        // } else {
                        //     resolve(undefined);
                        // }
                        resolve(resultData);
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //通过题目公布答案
    publicRightAnswer(SendSubjectID, CorrectAnswer, OnlineClassID, moocCourseID) {
        let keymap = {
            method: 'GET',
            headers: { 'SendSubjectID': SendSubjectID, 'CorrectAnswer': encodeURI(CorrectAnswer), 'OnlineClassID': OnlineClassID, 'moocCourseID': moocCourseID, },
        };
        console.log('arg', keymap)
        let correctAnswer;
        var promise = new Promise((resolve, reject) => {
            fetch(URL.pushAnswer_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        console.log('公布答案')
                        console.log(resultData)
                        resolve(resultData);
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        return promise;
    }
    //获取当前地理位置
    getCurrentPosition() {
        var promise = new Promise((resolve, reject) => {
            Geolocation.getCurrentPosition().then((data) => {
                // Alert.alert('温馨提醒:', data.latitude);
                resolve(data);
            }).catch((err) => { reject(err) })
        })
        return promise;
    }

}



